package com.stylefeng.guns.modular.system.controller;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.cache.CacheKit;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.validater.Update;
import com.stylefeng.guns.modular.system.model.PostPath;
import com.stylefeng.guns.modular.system.service.IPostPathService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 系统岗位层级关系控制器
 *
 * @author fengshuonan
 * @Date 2018-09-13 14:25:35
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/postPath"})
@Api(value = "系统岗位层级接口类", tags = {"系统岗位层级接口类"})
public class PostPathController extends BaseController {

    private String PREFIX = "/system/postPath/";

    @Autowired
    private IPostPathService postPathService;

    /**
     * 跳转到系统岗位层级关系首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "postPath.html";
    }

    /**
     * 跳转到添加系统岗位层级关系
     */
    @RequestMapping("/postPath_add")
    public String postPathAdd() {
        return PREFIX + "postPath_add.html";
    }

    /**
     * 跳转到修改系统岗位层级关系
     */
    @RequestMapping("/postPath_update/{postPathId}")
    public String postPathUpdate(@PathVariable Integer postPathId, Model model) {
        PostPath postPath = postPathService.selectById(postPathId);
        model.addAttribute("item",postPath);
        LogObjectHolder.me().set(postPath);
        return PREFIX + "postPath_edit.html";
    }

    /**
     * 获取系统岗位层级关系列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(@ApiParam(name = "spId", value = "所属机构ID", type = "String", required = false) @RequestParam("spId") String spId) {
        Wrapper wrapper= new EntityWrapper<PostPath>();
        wrapper.eq("sp_id",spId);
        return Result.success(postPathService.selectList(wrapper));
    }

    /**
     * 新增系统岗位层级关系
     */
    @ApiOperation(value = "添加系统岗位层级", notes = "添加系统岗位层级", httpMethod = "POST")
    @RequestMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @Permission
    public Tip add(@Valid @RequestBody @ApiParam(name = "postPath", value = "新增岗位层级关系", required = true) PostPath postPath, BindingResult result) {
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        ShiroUser shiroUser = this.getShiroUser();
        postPath.setCreateTime(DateTime.now());
        postPath.setCreateUser(StringUtils.join(shiroUser.getAccount(),"|",shiroUser.getName()));
        postPath.setSoId(shiroUser.getDeptId());
        postPathService.insert(postPath);
        return SUCCESS_TIP;
    }

    /**
     * 删除系统岗位层级关系
     */
    @ApiOperation(value = "删除系统岗位层级", notes = "删除系统岗位层级", httpMethod = "POST")
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Tip delete(@RequestParam(value="postPathId") String postPathId) {
        postPathService.deleteById(postPathId);
        return SUCCESS_TIP;
    }

    /**
     * 修改系统岗位层级关系
     */
    @ApiOperation(value = "修改系统岗位层级关系", notes = "修改系统岗位层级关系", httpMethod = "POST")
    @PostMapping(value = "/update")
    @ResponseBody
    public Tip update(@Validated(value = Update.class)  @RequestBody  PostPath postPath, BindingResult result) {
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        this.postPathService.updateById(postPath);
        return SUCCESS_TIP;
    }

    /**
     * 系统岗位层级关系详情
     */
    @RequestMapping(value = "/detail/{postPathId}")
    @ResponseBody
    public Result detail(@PathVariable("postPathId") Integer postPathId) {
        return Result.success(postPathService.selectById(postPathId));
    }

    @Cacheable(value="local",key = "'userCw_'+#result.getId()")
    public  ShiroUser getShiroUser(){
        String token= getHttpServletRequest().getHeader("Authorization").substring(7);
        return CacheKit.get(CACHE_STRATEGY,token);
    }
}
